package com.member.web.controller.shop;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.member.common.utils.DateUtils;
import com.member.shop.domain.SysBizCouponGrantUser;
import com.member.shop.domain.SysBizShopCertification;
import com.member.shop.service.ISysBizShopCertificationService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import javax.annotation.Resource;

import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.member.common.annotation.Log;
import com.member.common.core.controller.BaseController;
import com.member.common.core.domain.AjaxResult;
import com.member.common.enums.BusinessType;
import com.member.shop.domain.SysBizCouponReceive;
import com.member.shop.service.ISysBizCouponReceiveService;
import com.member.common.utils.poi.ExcelUtil;
import com.member.common.core.page.TableDataInfo;

/**
 * 【优惠券用户领取记录】控制器
 *
 * @author lixinchen
 * @version 1.0
 * @date 2024-05-08
 **/
@RestController
@RequestMapping("/shop/receive")
public class SysBizCouponReceiveController extends BaseController {
    @Resource
    private ISysBizCouponReceiveService sysBizCouponReceiveService;

    @Resource
    private ISysBizShopCertificationService sysBizShopCertificationService;

    private void setTenantId(SysBizCouponReceive sysBizCouponReceive) {
        Long tenantId = getTenantId();
        if (tenantId != null && tenantId > 0) {
            sysBizCouponReceive.setTenantId(tenantId);
        }
    }

    /**
     * 查询优惠券用户领取记录列表
     */
    @PreAuthorize("@ss.hasPermi('shop:receive:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysBizCouponReceive sysBizCouponReceive) {
        startPage();
        setTenantId(sysBizCouponReceive);
        List<SysBizCouponReceive> list = sysBizCouponReceiveService.selectSysBizCouponReceiveList(sysBizCouponReceive);

        if(!CollectionUtils.isEmpty(list)){
            for (SysBizCouponReceive bizCouponReceive : list) {
                List<SysBizShopCertification> sysBizShopCertification = sysBizShopCertificationService.selectSysBizShopCertificationList(
                        new LambdaQueryWrapper<SysBizShopCertification>().eq(SysBizShopCertification::getUserId,bizCouponReceive.getUserId())
                                .eq(SysBizShopCertification::getDeleted,0)
                );
                if (!CollectionUtils.isEmpty(sysBizShopCertification)) {
                    bizCouponReceive.setUserName(sysBizShopCertification.get(0).getShopName());
                }
            }

        }
        return getDataTable(list);
    }

    /**
     * 导出优惠券用户领取记录列表
     */
    @PreAuthorize("@ss.hasPermi('shop:receive:export')")
    @Log(title = "优惠券用户领取记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysBizCouponReceive sysBizCouponReceive) {
        setTenantId(sysBizCouponReceive);
        List<SysBizCouponReceive> list = sysBizCouponReceiveService.selectSysBizCouponReceiveList(sysBizCouponReceive);
        if (!CollectionUtils.isEmpty(list)) {
            for (SysBizCouponReceive bizCouponReceive : list) {
                if (bizCouponReceive.getThresholdAmount() != null && bizCouponReceive.getThresholdAmount().compareTo(BigDecimal.ZERO) > 0) {
                    bizCouponReceive.setThresholdAmountForExport("满￥" + bizCouponReceive.getThresholdAmount() + "可用");
                } else if (bizCouponReceive.getThresholdAmount() != null && bizCouponReceive.getThresholdAmount().compareTo(BigDecimal.ZERO) == 0) {
                    bizCouponReceive.setThresholdAmountForExport("无门槛");
                }
                if (bizCouponReceive.getValidityType() == 1) {
                    bizCouponReceive.setTermDateForExport(DateUtils.dateTime(bizCouponReceive.getTermBeginTime()) + "~" + DateUtils.dateTime(bizCouponReceive.getTermEndTime()));
                }
                if (bizCouponReceive.getValidityType() == 2) {
                    bizCouponReceive.setTermDateForExport("领取后" + bizCouponReceive.getReceivingDay() + "天生效，生效后" + bizCouponReceive.getEffectiveDay() + "天失效");
                }
            }
        }
        ExcelUtil<SysBizCouponReceive> util = new ExcelUtil<SysBizCouponReceive>(SysBizCouponReceive.class);
        util.exportExcel(response, list, "优惠券用户领取记录数据");
    }

    /**
     * 获取优惠券用户领取记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('shop:receive:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sysBizCouponReceiveService.selectSysBizCouponReceiveById(id));
    }

    /**
     * 新增优惠券用户领取记录
     */
    @PreAuthorize("@ss.hasPermi('shop:receive:add')")
    @Log(title = "优惠券用户领取记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysBizCouponReceive sysBizCouponReceive) {
        return toAjax(sysBizCouponReceiveService.insertSysBizCouponReceive(sysBizCouponReceive));
    }

    /**
     * 修改优惠券用户领取记录
     */
    @PreAuthorize("@ss.hasPermi('shop:receive:edit')")
    @Log(title = "优惠券用户领取记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysBizCouponReceive sysBizCouponReceive) {
        return toAjax(sysBizCouponReceiveService.updateSysBizCouponReceive(sysBizCouponReceive));
    }

    /**
     * 删除优惠券用户领取记录
     */
    @PreAuthorize("@ss.hasPermi('shop:receive:remove')")
    @Log(title = "优惠券用户领取记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysBizCouponReceiveService.deleteSysBizCouponReceiveByIds(ids));
    }
}
